﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using cndp.Model;

namespace cndp.View
{
    public partial class FormMain : Form, IFormMain
    {
        #region Methods

        /// <summary>
        ///     Om en dykares information ändras så skall även
        ///     denna information ändras för varje tävling
        ///     denne är med i.
        /// </summary>
        private void UpdateDiverChanges()
        {
            if ((EventGetEventList != null) && (EventGetContest != null))
            {
                for (var diverRegisteredIndex = 0;
                    diverRegisteredIndex < (EventGetContest(_rowOfContest).DiverRegister.Count - 1);
                    diverRegisteredIndex++)
                {
                    for (var eventIndex = 0; eventIndex < EventGetEventList(_rowOfContest).Count; eventIndex++)
                    {
                        var diverFound =
                            EventGetEventList(_rowOfContest)[eventIndex].DiverList.SingleOrDefault(
                                d =>
                                    d.Diver.PersonalCodeNumber ==
                                    EventGetContest(_rowOfContest).DiverRegister[diverRegisteredIndex]
                                        .PersonalCodeNumber);
                        if (diverFound != null)
                        {
                            diverFound.Diver = EventGetContest(_rowOfContest).DiverRegister[diverRegisteredIndex];
                        }
                    }
                }
            }
        }

        /// <summary>
        ///     Om en dykare tas bort från registret så skall
        ///     den även tas bort från varje gren.
        /// </summary>
        private void UpdateDiverDeletion()
        {
            if ((EventGetEventList != null) && (EventGetContest != null))
            {
                for (var eventIndex = 0; eventIndex < EventGetEventList(_rowOfContest).Count; eventIndex++)
                {
                    for (var diverIndex = 0;
                        diverIndex < EventGetEventList(_rowOfContest)[eventIndex].DiverList.Count;
                        diverIndex++)
                    {
                        var diverFound =
                            EventGetContest(_rowOfContest)
                                .DiverRegister.SingleOrDefault(
                                    d =>
                                        d.PersonalCodeNumber ==
                                        EventGetEventList(_rowOfContest)[eventIndex].DiverList[diverIndex].Diver
                                            .PersonalCodeNumber);
                        if (diverFound == null)
                        {
                            EventGetEventList(_rowOfContest)[eventIndex].DiverList.RemoveAt(diverIndex);
                        }
                    }
                }
            }
        }

        /// <summary>
        ///     Om en domares information ändras så skall även
        ///     denna information ändras för varje tävling
        ///     denne är med i.
        /// </summary>
        private void UpdateJudgeChanges()
        {
            if ((EventGetEventList != null) && (EventGetContest != null))
            {
                for (var judgeRegisteredIndex = 0;
                    judgeRegisteredIndex < (EventGetContest(_rowOfContest).JudgeRegister.Count - 1);
                    judgeRegisteredIndex++)
                {
                    for (var eventIndex = 0; eventIndex < EventGetEventList(_rowOfContest).Count; eventIndex++)
                    {
                        var judgeFound =
                            EventGetEventList(_rowOfContest)[eventIndex].JudgeList.SingleOrDefault(
                                j =>
                                    j.PersonalCodeNumber ==
                                    EventGetContest(_rowOfContest).JudgeRegister[judgeRegisteredIndex]
                                        .PersonalCodeNumber);
                        if (judgeFound != null)
                        {
                            judgeFound.Name = EventGetContest(_rowOfContest).JudgeRegister[judgeRegisteredIndex].Name;
                            judgeFound.Nationality =
                                EventGetContest(_rowOfContest).JudgeRegister[judgeRegisteredIndex].Nationality;
                            judgeFound.Affilliate =
                                EventGetContest(_rowOfContest).JudgeRegister[judgeRegisteredIndex].Affilliate;
                            judgeFound.Gender =
                                EventGetContest(_rowOfContest).JudgeRegister[judgeRegisteredIndex].Gender;
                            judgeFound.PersonalCodeNumber =
                                EventGetContest(_rowOfContest).JudgeRegister[judgeRegisteredIndex].PersonalCodeNumber;
                        }
                    }
                }
            }
        }

        /// <summary>
        ///     Om en domare tas bort från registret så skall
        ///     den även tas bort från varje gren.
        /// </summary>
        private void UpdateJudgeDeletion()
        {
            if ((EventGetEventList != null) && (EventGetContest != null))
            {
                for (var eventIndex = 0; eventIndex < EventGetEventList(_rowOfContest).Count; eventIndex++)
                {
                    for (var judgeIndex = 0;
                        judgeIndex < EventGetEventList(_rowOfContest)[eventIndex].JudgeList.Count;
                        judgeIndex++)
                    {
                        var judgeFound =
                            EventGetContest(_rowOfContest)
                                .JudgeRegister.SingleOrDefault(
                                    j =>
                                        j.PersonalCodeNumber ==
                                        EventGetEventList(_rowOfContest)[eventIndex].JudgeList[judgeIndex]
                                            .PersonalCodeNumber);
                        if (judgeFound == null)
                        {
                            EventGetEventList(_rowOfContest)[eventIndex].JudgeList.RemoveAt(judgeIndex);
                        }
                    }
                }
            }
        }

        private void AdaptToScreenResolution()
        {
            var height = Screen.PrimaryScreen.Bounds.Height;
            var width = Screen.PrimaryScreen.Bounds.Width;

            #region Buttons

            buttonCreateContest.Location = new Point((int) (width/14.22916666666667), (int) (height/6.047244094488189));
            buttonEditContest.Location = new Point(buttonCreateContest.Right + 5, buttonCreateContest.Location.Y);
            buttonDeleteContest.Location = new Point(buttonEditContest.Right + 5, buttonEditContest.Location.Y);

            labelPlannedContests.Location = new Point(buttonCreateContest.Location.X, buttonCreateContest.Bottom + 3);
            dataGridViewContestList.Location = new Point(buttonCreateContest.Location.X, labelPlannedContests.Bottom + 3);

            buttonSaveContestListToXmlFile.Location = new Point(buttonCreateContest.Location.X,
                dataGridViewContestList.Bottom + 5);
            buttonReadContestListFromXmlFile.Location = new Point(buttonCreateContest.Location.X,
                buttonSaveContestListToXmlFile.Bottom + 5);
            buttonReadContestFromXmlFile.Location = new Point(buttonCreateContest.Location.X,
                buttonReadContestListFromXmlFile.Bottom + 5);

            buttonHelp.Location = new Point((int) (width/3.869688385269122), (int) (height/6.144));
            buttonInfo.Location = new Point(buttonHelp.Right + 5, buttonHelp.Location.Y);

            dataGridViewContestInformation.Location = new Point((int) (width/2.956709956709957),
                (int) (height/4.266666666666667));

            labelNumberOfDiversText.Location = new Point((int) (width/3.78393351800554),
                (int) (height/3.096774193548387));
            labelNumberOfDivers.Location = new Point(labelNumberOfDiversText.Right - 6,
                labelNumberOfDiversText.Location.Y);
            buttonShowAndEditDivers.Location = new Point(labelNumberOfDivers.Right + 6,
                labelNumberOfDiversText.Location.Y);

            buttonShowAndEditJudges.Location = new Point(buttonShowAndEditDivers.Location.X,
                buttonShowAndEditDivers.Bottom + 11);
            labelNumberOfJudges.Location = new Point(buttonShowAndEditJudges.Left - 29,
                buttonShowAndEditJudges.Location.Y);
            labelNumberOfJudgesText.Location = new Point(labelNumberOfJudges.Left - 128, labelNumberOfJudges.Location.Y);

            buttonAddDiver.Location = new Point(labelNumberOfJudgesText.Location.X, labelNumberOfJudgesText.Bottom + 35);
            buttonAddJudge.Location = new Point(buttonAddDiver.Location.X, buttonAddDiver.Location.Y + 90);
            buttonSaveContestToXmlFile.Location = new Point(buttonAddJudge.Location.X, buttonAddJudge.Location.Y + 90);
            buttonStartContest.Location = new Point(buttonSaveContestToXmlFile.Location.X,
                buttonSaveContestToXmlFile.Location.Y + 90);

            buttonAddEventToContest.Location = new Point(buttonShowAndEditDivers.Right + 47,
                buttonShowAndEditDivers.Location.Y);
            buttonDeleteEventFromContest.Location = new Point(buttonAddEventToContest.Right + 6,
                buttonAddEventToContest.Location.Y);
            buttonEditEvent.Location = new Point(buttonDeleteEventFromContest.Right + 6,
                buttonDeleteEventFromContest.Location.Y);
            buttonAddDiverToEvent.Location = new Point(buttonEditEvent.Right + 6, buttonEditEvent.Location.Y);
            buttonAddJudgeToEvent.Location = new Point(buttonAddDiverToEvent.Right + 6, buttonAddDiverToEvent.Location.Y);
            dataGridViewEventList.Location = new Point(buttonAddEventToContest.Location.X,
                buttonAddDiverToEvent.Bottom + 6);

            buttonCloseProgram.Location = new Point((int) (width/1.068021892103206), (height/64));
            buttonShowOld.Location = new Point(buttonCloseProgram.Location.X - 149, buttonCloseProgram.Location.Y);

            #endregion

            #region InvisibleLabels

            labelinvisibleCreateContest.Location = new Point(buttonCreateContest.Right - 10,
                buttonCreateContest.Location.Y + 10);
            labelinvisibleEditContest.Location = new Point(buttonEditContest.Right - 10,
                buttonEditContest.Location.Y + 10);
            labelinvisibleDeleteContest.Location = new Point(buttonDeleteContest.Right - 10,
                buttonDeleteContest.Location.Y + 10);
            labelinvisibleHelp.Location = new Point(buttonHelp.Right - 10, buttonHelp.Location.Y + 10);
            labelinvisibleInfo.Location = new Point(buttonInfo.Right - 10, buttonInfo.Location.Y + 10);
            labelinvisibleShowAndEditDivers.Location = new Point(buttonShowAndEditDivers.Right - 10,
                buttonShowAndEditDivers.Location.Y + 15);
            labelinvisibleShowAndEditJudges.Location = new Point(buttonShowAndEditJudges.Right - 10,
                buttonShowAndEditJudges.Location.Y + 15);

            labelinvisibleAddDiver.Location = new Point(buttonAddDiver.Right - 10, buttonAddDiver.Location.Y + 55);
            labelinvisibleAddJudge.Location = new Point(buttonAddJudge.Right - 10, buttonAddJudge.Location.Y + 55);
            labelinvisibleSaveContestToXmlFile.Location = new Point(buttonSaveContestToXmlFile.Right - 10,
                buttonSaveContestToXmlFile.Location.Y + 55);
            labelinvisibleStartContest.Location = new Point(buttonStartContest.Right - 10,
                buttonStartContest.Location.Y + 55);

            labelinvisibleEscape.Location = new Point(buttonCloseProgram.Right - 13, buttonCloseProgram.Location.Y + 10);
            labelinvisibleShowOldContests.Location = new Point(buttonShowOld.Right - 10, buttonShowOld.Location.Y + 10);

            labelinvisibleAddEventToContest.Location = new Point(buttonAddEventToContest.Right - 10,
                buttonAddEventToContest.Location.Y + 30);
            labelinvisibleRemoveEventFromContest.Location = new Point(buttonDeleteEventFromContest.Right - 10,
                buttonDeleteEventFromContest.Location.Y + 30);
            labelinvisibleEditEvent.Location = new Point(buttonEditEvent.Right - 10, buttonEditEvent.Location.Y + 30);
            labelinvisibleAddDiverToEvent.Location = new Point(buttonAddDiverToEvent.Right - 10,
                buttonAddDiverToEvent.Location.Y + 30);
            labelinvisibleAddJudgeToEvent.Location = new Point(buttonAddJudgeToEvent.Right - 10,
                buttonAddJudgeToEvent.Location.Y + 30);

            labelinvisibleDataGridViewEventList.Location = new Point(dataGridViewEventList.Right - 125,
                dataGridViewEventList.Location.Y + 376);

            labelinvisibleDataGridViewContestList.Location = new Point(dataGridViewContestList.Right - 100,
                dataGridViewContestList.Location.Y + 310);
            labelinvisibleSaveContestListToXmlFile.Location = new Point(buttonSaveContestListToXmlFile.Right - 10,
                buttonSaveContestListToXmlFile.Location.Y + 25);
            labelinvisibleReadContestListFromXmlFile.Location = new Point(buttonReadContestListFromXmlFile.Right - 10,
                buttonReadContestListFromXmlFile.Location.Y + 25);
            labelinvisibleReadContestFromXmlFile.Location = new Point(buttonReadContestFromXmlFile.Right - 10,
                buttonReadContestFromXmlFile.Location.Y + 25);

            #endregion
        }

        public bool CheckStartContest()
        {
            var checkStartContest = true;
            if (EventGetContest != null)
            {
                if (EventGetContest(_rowOfContest).EventList.Count <= 0)
                {
                    MessageBox.Show(@"Din tävling behöver grenar");
                    checkStartContest = false;
                }
            }

            if (EventGetEventList != null)
            {
                for (var eventIndex = 0; eventIndex <= (EventGetEventList(_rowOfContest).Count - 1); eventIndex++)
                {
                    if (EventGetEventList(_rowOfContest)[eventIndex].JudgeList.Count !=
                        EventGetEventList(_rowOfContest)[eventIndex].MaxJudges)
                    {
                        MessageBox.Show(@"Grenen med index nummer " + (eventIndex + 1) +
                                        @"har inte tillräckligt med domare");
                        checkStartContest = false;
                    }

                    if (EventGetEventList(_rowOfContest)[eventIndex].JumpList.Count == 0)
                    {
                        MessageBox.Show(@"Grenen med index" + (eventIndex + 1) + @"har inga dykare");
                        checkStartContest = false;
                    }

                    for (var diverIndex = 0;
                        diverIndex < EventGetEventList(_rowOfContest)[eventIndex].DiverList.Count;
                        ++diverIndex)
                    {
                        if (
                            EventGetEventList(_rowOfContest)[eventIndex].DiverList[diverIndex].JumpCodesandStyle.Count !=
                            EventGetEventList(_rowOfContest)[eventIndex].Rounds)
                        {
                            MessageBox.Show(@"Samtliga hoppkoder för grenen med index nummer" + (eventIndex + 1) +
                                            @", är ej ifyllda");
                            checkStartContest = false;
                        }
                    }
                }
            }
            return checkStartContest;
        }

        #endregion

        #region Variabels

        private int _rowOfContest = -1;
        private int _rowOfEvent = -1;
        private bool _showOldContests;

        #endregion

        #region Constructor&Load

        public FormMain()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            KeyPreview = true;
            labelinvisibleShowOldContests.Visible = false;
            labelinvisibleSaveContestToXmlFile.Visible = false;
            labelinvisibleCreateContest.Visible = false;
            labelinvisibleEditContest.Visible = false;
            labelinvisibleDeleteContest.Visible = false;
            labelinvisibleHelp.Visible = false;
            labelinvisibleInfo.Visible = false;
            labelinvisibleShowAndEditDivers.Visible = false;
            labelinvisibleShowAndEditJudges.Visible = false;
            labelinvisibleAddDiver.Visible = false;
            labelinvisibleAddJudge.Visible = false;
            labelinvisibleStartContest.Visible = false;
            labelinvisibleSaveContestListToXmlFile.Visible = false;
            labelinvisibleReadContestListFromXmlFile.Visible = false;
            labelinvisibleAddEventToContest.Visible = false;
            labelinvisibleRemoveEventFromContest.Visible = false;
            labelinvisibleEditEvent.Visible = false;
            labelinvisibleAddDiverToEvent.Visible = false;
            labelinvisibleAddJudgeToEvent.Visible = false;
            labelinvisibleReadContestFromXmlFile.Visible = false;
            labelinvisibleDataGridViewEventList.Visible = false;
            labelinvisibleDataGridViewContestList.Visible = false;
            labelinvisibleEscape.Visible = false;

            AdaptToScreenResolution();

            if (dataGridViewContestList.Rows.Count != 0)
            {
                _rowOfContest = 0;
                dataGridViewContestList_CellClick(dataGridViewContestList, new DataGridViewCellEventArgs(0, 0));
                dataGridViewContestList.Rows[_rowOfContest].Selected = true;

                if (dataGridViewEventList.Rows.Count != 0)
                {
                    _rowOfEvent = 0;
                    dataGridViewEventList_CellClick(dataGridViewEventList, new DataGridViewCellEventArgs(0, 0));
                }
            }
        }

        #endregion

        #region EventClicks

        private void FormMain_KeyDown(object sender, KeyEventArgs e)
        {
            if (ModifierKeys == Keys.Control & e.KeyCode == Keys.Tab)
            {
                labelinvisibleShowOldContests.Visible = !labelinvisibleShowOldContests.Visible;
                labelinvisibleSaveContestToXmlFile.Visible = !labelinvisibleSaveContestToXmlFile.Visible;
                labelinvisibleCreateContest.Visible = !labelinvisibleCreateContest.Visible;
                labelinvisibleEditContest.Visible = !labelinvisibleEditContest.Visible;
                labelinvisibleDeleteContest.Visible = !labelinvisibleDeleteContest.Visible;
                labelinvisibleHelp.Visible = !labelinvisibleHelp.Visible;
                labelinvisibleInfo.Visible = !labelinvisibleInfo.Visible;
                labelinvisibleShowAndEditDivers.Visible = !labelinvisibleShowAndEditDivers.Visible;
                labelinvisibleShowAndEditJudges.Visible = !labelinvisibleShowAndEditJudges.Visible;
                labelinvisibleAddDiver.Visible = !labelinvisibleAddDiver.Visible;
                labelinvisibleAddJudge.Visible = !labelinvisibleAddJudge.Visible;
                labelinvisibleStartContest.Visible = !labelinvisibleStartContest.Visible;
                labelinvisibleAddEventToContest.Visible = !labelinvisibleAddEventToContest.Visible;
                labelinvisibleRemoveEventFromContest.Visible = !labelinvisibleRemoveEventFromContest.Visible;
                labelinvisibleEditEvent.Visible = !labelinvisibleEditEvent.Visible;
                labelinvisibleAddDiverToEvent.Visible = !labelinvisibleAddDiverToEvent.Visible;
                labelinvisibleAddJudgeToEvent.Visible = !labelinvisibleAddJudgeToEvent.Visible;
                labelinvisibleReadContestFromXmlFile.Visible = !labelinvisibleReadContestFromXmlFile.Visible;
                labelinvisibleDataGridViewContestList.Visible = !labelinvisibleDataGridViewContestList.Visible;
                labelinvisibleDataGridViewEventList.Visible = !labelinvisibleDataGridViewEventList.Visible;
                labelinvisibleSaveContestListToXmlFile.Visible = !labelinvisibleSaveContestListToXmlFile.Visible;
                labelinvisibleReadContestListFromXmlFile.Visible = !labelinvisibleReadContestListFromXmlFile.Visible;
                labelinvisibleEscape.Visible = !labelinvisibleEscape.Visible;
            }
            if (e.KeyCode == Keys.Escape)
                Close();
            if (ModifierKeys == Keys.Control & e.KeyCode == Keys.D1)
                buttonCreateContest.PerformClick();
            if (ModifierKeys == Keys.Control & e.KeyCode == Keys.D2)
                buttonEditContest.PerformClick();
            if (ModifierKeys == Keys.Control & e.KeyCode == Keys.D3)
                buttonDeleteContest.PerformClick();
            if (ModifierKeys == Keys.Control & e.KeyCode == Keys.D4)
                buttonHelp.PerformClick();
            if (ModifierKeys == Keys.Control & e.KeyCode == Keys.D5)
                buttonInfo.PerformClick();
            if (ModifierKeys == Keys.Control & e.KeyCode == Keys.D6)
                buttonShowAndEditDivers.PerformClick();
            if (ModifierKeys == Keys.Control & e.KeyCode == Keys.D7)
                buttonShowAndEditJudges.PerformClick();
            if (ModifierKeys == Keys.Control & e.KeyCode == Keys.D8)
                buttonAddDiver.PerformClick();
            if (ModifierKeys == Keys.Control & e.KeyCode == Keys.D9)
                buttonAddJudge.PerformClick();
            if (ModifierKeys == Keys.Control & e.KeyCode == Keys.D0)
                buttonSaveContestToXmlFile.PerformClick();
            if (ModifierKeys == Keys.Control & e.KeyCode == Keys.Oemplus)
                buttonStartContest.PerformClick();
            if (ModifierKeys == Keys.Control & e.KeyCode == Keys.L)
                buttonReadContestListFromXmlFile.PerformClick();
            if (ModifierKeys == Keys.Control & e.KeyCode == Keys.K)
                buttonReadContestFromXmlFile.PerformClick();
            if (ModifierKeys == Keys.Control & e.KeyCode == Keys.S)
                buttonSaveContestListToXmlFile.PerformClick();
            if (ModifierKeys == Keys.Control & e.KeyCode == Keys.Z)
                buttonAddEventToContest.PerformClick();
            if (ModifierKeys == Keys.Control & e.KeyCode == Keys.X)
                buttonDeleteEventFromContest.PerformClick();
            if (ModifierKeys == Keys.Control & e.KeyCode == Keys.C)
                buttonEditEvent.PerformClick();
            if (ModifierKeys == Keys.Control & e.KeyCode == Keys.V)
                buttonAddDiverToEvent.PerformClick();
            if (ModifierKeys == Keys.Control & e.KeyCode == Keys.B)
                buttonAddJudgeToEvent.PerformClick();
            if (ModifierKeys == Keys.Control & e.KeyCode == Keys.O)
                buttonShowOld.PerformClick();
            if (ModifierKeys == Keys.Control & e.KeyCode == Keys.Down)
            {
                if (_rowOfContest != (dataGridViewContestList.Rows.Count - 1))
                {
                    dataGridViewContestList.Rows[_rowOfContest].Selected = false;
                    _rowOfContest++;
                    dataGridViewContestList_CellClick(dataGridViewContestList,
                        new DataGridViewCellEventArgs(0, _rowOfContest));
                    dataGridViewContestList.Rows[_rowOfContest].Selected = true;

                    if (dataGridViewEventList.Rows.Count == 0)
                    {
                        _rowOfEvent = -1;
                    }
                    else
                    {
                        _rowOfEvent = 0;
                        dataGridViewEventList_CellClick(dataGridViewEventList, new DataGridViewCellEventArgs(0, 0));
                    }
                }
            }
            if (ModifierKeys == Keys.Control & e.KeyCode == Keys.Up)
            {
                if ((_rowOfContest != 0) && (_rowOfContest != -1))
                {
                    dataGridViewContestList.Rows[_rowOfContest].Selected = false;
                    _rowOfContest--;
                    dataGridViewContestList_CellClick(dataGridViewContestList, new DataGridViewCellEventArgs(0, _rowOfContest));
                    dataGridViewContestList.Rows[_rowOfContest].Selected = true;
                }
                if (dataGridViewEventList.Rows.Count == 0)
                {
                    _rowOfEvent = -1;
                }
                else
                {
                    _rowOfEvent = 0;
                    dataGridViewEventList_CellClick(dataGridViewEventList, new DataGridViewCellEventArgs(0, 0));
                }
            }
            if (ModifierKeys == Keys.Control & e.KeyCode == Keys.Left)
            {
                if ((_rowOfEvent != 0) && (_rowOfEvent != -1))
                {
                    dataGridViewEventList.Rows[_rowOfEvent].Selected = false;
                    _rowOfEvent--;
                    dataGridViewEventList_CellClick(dataGridViewEventList, new DataGridViewCellEventArgs(0, _rowOfEvent));
                    dataGridViewEventList.Rows[_rowOfEvent].Selected = true;
                }
            }
            if (ModifierKeys == Keys.Control & e.KeyCode == Keys.Right)
            {
                if (_rowOfEvent != dataGridViewEventList.Rows.Count - 1)
                {
                    dataGridViewEventList.Rows[_rowOfEvent].Selected = false;
                    _rowOfEvent++;
                    dataGridViewEventList_CellClick(dataGridViewEventList, new DataGridViewCellEventArgs(0, _rowOfEvent));
                    dataGridViewEventList.Rows[_rowOfEvent].Selected = true;
                }
            }
        }

        private void dataGridViewContestList_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                _rowOfContest = Convert.ToInt16(dataGridViewContestInformation.CurrentCell.RowIndex);
                dataGridViewContestList_CellClick(dataGridViewContestList,
                    new DataGridViewCellEventArgs(dataGridViewContestInformation.CurrentCell.ColumnIndex,
                        dataGridViewContestInformation.CurrentCell.RowIndex));
            }
        }

        private void buttonReadContestFromXmlFile_Click(object sender, EventArgs e)
        {
            var formReadContestFromFile = new FormSaveAndReadContestAndContestListToAndFromFile
            {
                Text = @"Läs tävling från fil",
                Type = "READ"
            };

            formReadContestFromFile.ShowDialog();
            if ((EventReadContestFromXmlFile != null) && (formReadContestFromFile.DialogResult == DialogResult.OK) &&
                (EventGetContestList != null))
            {
                EventGetContestList().Add(EventReadContestFromXmlFile(formReadContestFromFile.FileName + ".xml"));
            }
        }

        private void buttonShowOld_Click(object sender, EventArgs e)
        {
            _showOldContests = !_showOldContests;
            if (_showOldContests)
            {
                if ((EventSaveContestListFromXmlFile != null) && (EventGetOldContestList != null))
                {
                    EventSaveContestListFromXmlFile(@"../../view/tempcontestlist.xml");
                    EventGetOldContestList().Clear();
                }
                if (EventReadOldContestListFromXmlFile != null)
                    EventReadOldContestListFromXmlFile(@"../../view/oldcontests.xml");
                dataGridViewContestList.Rows.Clear();
                buttonAddDiver.Enabled = false;
                buttonAddJudge.Enabled = false;
                buttonSaveContestListToXmlFile.Enabled = false;
                buttonReadContestFromXmlFile.Enabled = false;
                buttonStartContest.Enabled = false;
                buttonCreateContest.Enabled = false;
                buttonEditContest.Enabled = false;
                buttonDeleteContest.Enabled = false;
                buttonAddEventToContest.Enabled = false;
                buttonDeleteEventFromContest.Enabled = false;
                buttonEditEvent.Enabled = false;
                buttonAddDiverToEvent.Enabled = false;
                buttonAddJudgeToEvent.Enabled = false;
                buttonSaveContestToXmlFile.Enabled = false;
                buttonReadContestListFromXmlFile.Enabled = false;
                labelPlannedContests.Text = @"Avslutade tävlingar.";
                buttonShowOld.Text = @"Visa planerade tävlingar";
                buttonShowAndEditDivers.Text = @"Visa";
                buttonShowAndEditJudges.Text = @"Visa";
                if (EventGetOldContestList != null)
                {
                    dataGridViewContestList.DataSource = EventGetOldContestList();
                    if ((EventGetOldContestList().Count != 0) && (EventGetOldNumberOfDivers != null) && (EventGetOldNumberOfJudges != null) && (EventGetOldContest != null))
                    {
                        labelNumberOfDivers.Text = EventGetOldNumberOfDivers(0).ToString();
                        labelNumberOfJudges.Text = EventGetOldNumberOfJudges(0).ToString();
                        dataGridViewContestInformation[0, 0].Value = EventGetOldContest(0).Name;
                        dataGridViewContestInformation[1, 0].Value = EventGetOldContest(0).Id;
                        dataGridViewContestInformation[2, 0].Value = EventGetOldContest(0).Location;
                        dataGridViewContestInformation[3, 0].Value = EventGetOldContest(0).StartTime;
                        dataGridViewContestInformation[4, 0].Value = EventGetOldContest(0).FinishTime;
                        dataGridViewContestInformation[5, 0].Value = EventGetOldContest(0).DeadlineDate;
                        dataGridViewContestInformation[6, 0].Value = EventGetOldContest(0).ContestAdministrator;
                    }
                }
                else
                {
                    MessageBox.Show(@"Det finns inga avslutade tävlingar.");
                    _showOldContests = false;
                }
            }
            if (!_showOldContests)
            {
                if (EventReadContestListFromXmlFile != null)
                    EventReadContestListFromXmlFile(@"../../view/tempcontestlist.xml");
                dataGridViewContestList.Rows.Clear();
                if (EventGetContestList != null)
                    dataGridViewContestList.DataSource = EventGetContestList();
                buttonShowOld.Text = @"Visa gamla tävlingar";
                buttonShowAndEditDivers.Text = @"Visa/Ändra";
                buttonShowAndEditJudges.Text = @"Visa/Ändra";
                buttonAddDiver.Enabled = true;
                buttonAddJudge.Enabled = true;
                buttonSaveContestListToXmlFile.Enabled = true;
                buttonReadContestFromXmlFile.Enabled = true;
                buttonStartContest.Enabled = true;
                buttonCreateContest.Enabled = true;
                buttonEditContest.Enabled = true;
                buttonDeleteContest.Enabled = true;
                buttonAddEventToContest.Enabled = true;
                buttonDeleteEventFromContest.Enabled = true;
                buttonEditEvent.Enabled = true;
                buttonAddDiverToEvent.Enabled = true;
                buttonAddJudgeToEvent.Enabled = true;
                buttonSaveContestToXmlFile.Enabled = true;
                buttonReadContestListFromXmlFile.Enabled = true;
                labelPlannedContests.Text = @"Planerade tävlingar.";
                if ((EventGetContestList != null) && (EventGetNumberOfDivers != null) && (EventGetNumberOfJudges != null) && (EventGetContest != null))
                {
                    labelNumberOfDivers.Text = EventGetNumberOfDivers(0).ToString();
                    labelNumberOfJudges.Text = EventGetNumberOfJudges(0).ToString();
                    dataGridViewContestInformation[0, 0].Value = EventGetContest(0).Name;
                    dataGridViewContestInformation[1, 0].Value = EventGetContest(0).Id;
                    dataGridViewContestInformation[2, 0].Value = EventGetContest(0).Location;
                    dataGridViewContestInformation[3, 0].Value = EventGetContest(0).StartTime;
                    dataGridViewContestInformation[4, 0].Value = EventGetContest(0).FinishTime;
                    dataGridViewContestInformation[5, 0].Value = EventGetContest(0).DeadlineDate;
                    dataGridViewContestInformation[6, 0].Value = EventGetContest(0).ContestAdministrator;
                }
            }
        }

        private void buttonCreateContest_Click(object sender, EventArgs e)
        {
            var formNewContest = new FormNewContest
            {
                ContestList = EventGetContestList(),
            };
       
            formNewContest.ShowDialog();
            if (formNewContest.Id != -1)
            {
                if (EventNewContest != null)
                {
                    EventNewContest(formNewContest.ContestName, formNewContest.Id, formNewContest.ContestLocation,
                        formNewContest.StartTime,
                        formNewContest.FinishTime, formNewContest.DeadlineDate, formNewContest.ContestAdministrator);
                }
            }
        }

        private void dataGridViewContestList_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (!_showOldContests)
            {
                if (dataGridViewContestList.Rows.Count != 0)
                {
                    dataGridViewContestList.Rows[_rowOfContest].Selected = false;
                    _rowOfContest = e.RowIndex;
                    dataGridViewContestList.Rows[_rowOfContest].Selected = true;
                    if (_rowOfContest != -1)
                    {
                        if (EventGetNumberOfDivers != null)
                            labelNumberOfDivers.Text = EventGetNumberOfDivers(_rowOfContest).ToString();
                        if (EventGetNumberOfJudges != null)
                            labelNumberOfJudges.Text = EventGetNumberOfJudges(_rowOfContest).ToString();

                        if (EventGetContest != null)
                        {
                            dataGridViewContestInformation[0, 0].Value = EventGetContest(_rowOfContest).Name;
                            dataGridViewContestInformation[1, 0].Value = EventGetContest(_rowOfContest).Id;
                            dataGridViewContestInformation[2, 0].Value = EventGetContest(_rowOfContest).Location;
                            dataGridViewContestInformation[3, 0].Value = EventGetContest(_rowOfContest).StartTime;
                            dataGridViewContestInformation[4, 0].Value = EventGetContest(_rowOfContest).FinishTime;
                            dataGridViewContestInformation[5, 0].Value = EventGetContest(_rowOfContest).DeadlineDate;
                            dataGridViewContestInformation[6, 0].Value =
                                EventGetContest(_rowOfContest).ContestAdministrator;
                        }

                        if (EventGetEventList != null)
                        {
                            dataGridViewEventList.DataSource = EventGetEventList(_rowOfContest);
                        }

                        if (dataGridViewEventList.Rows.Count != 0)
                        {
                            _rowOfEvent = 0;
                            dataGridViewEventList_CellClick(dataGridViewEventList,
                                new DataGridViewCellEventArgs(0, _rowOfEvent));
                            dataGridViewEventList.Rows[_rowOfEvent].Selected = true;
                        }

                        dataGridViewContestInformation.Enabled = false;
                    }
                }
            }
            else
            {
                if (dataGridViewContestInformation.Rows.Count != 0)
                {
                    _rowOfContest = e.RowIndex;
                    if (EventGetOldNumberOfDivers != null)
                        labelNumberOfDivers.Text = EventGetOldNumberOfDivers(_rowOfContest).ToString();
                    if (EventGetOldNumberOfJudges != null)
                        labelNumberOfJudges.Text = EventGetOldNumberOfJudges(_rowOfContest).ToString();

                    if (EventGetOldContest != null)
                    {
                        dataGridViewContestInformation[0, 0].Value = EventGetOldContest(_rowOfContest).Name;
                        dataGridViewContestInformation[1, 0].Value = EventGetOldContest(_rowOfContest).Id;
                        dataGridViewContestInformation[2, 0].Value = EventGetOldContest(_rowOfContest).Location;
                        dataGridViewContestInformation[3, 0].Value = EventGetOldContest(_rowOfContest).StartTime;
                        dataGridViewContestInformation[4, 0].Value = EventGetOldContest(_rowOfContest).FinishTime;
                        dataGridViewContestInformation[5, 0].Value = EventGetOldContest(_rowOfContest).DeadlineDate;
                        dataGridViewContestInformation[6, 0].Value =
                            EventGetOldContest(_rowOfContest).ContestAdministrator;
                    }
                    if (EventGetOldEventList != null)
                    {
                        dataGridViewEventList.DataSource = EventGetOldEventList(_rowOfContest);
                    }
                }
            }
        }

        private void buttonDeleteContest_Click(object sender, EventArgs e)
        {
            if ((dataGridViewContestList.Rows.Count != 0) && (_rowOfContest != -1))
            {
                if (_rowOfContest != -1)
                {
                    if (EventDeleteContest != null)
                    {
                        EventDeleteContest(_rowOfContest);
                    }
                }
                if (_rowOfContest == dataGridViewContestList.Rows.Count)
                {
                    _rowOfContest--;
                    if (_rowOfContest != -1)
                    {
                        dataGridViewContestList.Rows[_rowOfContest].Selected = true;
                        dataGridViewContestList_CellClick(dataGridViewContestList,
                            new DataGridViewCellEventArgs(0, _rowOfContest));
                    }
                }
                else
                {
                    dataGridViewContestList.Rows[_rowOfContest].Selected = true;
                    dataGridViewContestList_CellClick(dataGridViewContestList,
                        new DataGridViewCellEventArgs(0, _rowOfContest));
                }
            }
        }

        private void buttonCloseProgram_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void buttonAddDiver_Click(object sender, EventArgs e)
        {
            if (dataGridViewContestList.Rows.Count != 0)
            {
                if (_rowOfContest != -1)
                {
                    var formAddDiver = new FormAddDiverAndJudge();
                    formAddDiver.TypeOfAdd = "diver";
                    formAddDiver.ShowDialog();

                    if (formAddDiver.DialogResult == DialogResult.OK)
                    {
                        if (EventAddDiverToRegister != null)
                        {
                            EventAddDiverToRegister(formAddDiver.PersonName, formAddDiver.Gender,
                                formAddDiver.PersonalCodeNumber,
                                formAddDiver.Nationality, formAddDiver.ClubOrAffilliate, _rowOfContest);
                        }
                        if (EventGetNumberOfDivers != null)
                        {
                            labelNumberOfDivers.Text = EventGetNumberOfDivers(_rowOfContest).ToString();
                        }
                        if (EventGetNumberOfJudges != null)
                        {
                            labelNumberOfJudges.Text = EventGetNumberOfJudges(_rowOfContest).ToString();
                        }
                    }
                }
            }
        }

        private void buttonAddJudge_Click(object sender, EventArgs e)
        {
            if (dataGridViewContestList.Rows.Count != 0)
            {
                if (_rowOfContest != -1)
                {
                    var formAddDiver = new FormAddDiverAndJudge();
                    formAddDiver.TypeOfAdd = "judge";
                    formAddDiver.ShowDialog();
                    if (formAddDiver.DialogResult == DialogResult.OK)
                    {
                        if (EventAddJudgeToRegister != null)
                        {
                            EventAddJudgeToRegister(formAddDiver.PersonName, formAddDiver.Gender,
                                formAddDiver.PersonalCodeNumber,
                                formAddDiver.Nationality, formAddDiver.ClubOrAffilliate, _rowOfContest);
                        }
                        if ((EventGetNumberOfDivers != null) && (EventGetNumberOfJudges != null))
                        {
                            labelNumberOfDivers.Text = EventGetNumberOfDivers(_rowOfContest).ToString();
                            labelNumberOfJudges.Text = EventGetNumberOfJudges(_rowOfContest).ToString();
                        }
                    }
                }
            }
        }

        private void buttonShowDivers_Click(object sender, EventArgs e)
        {
            if (!_showOldContests)
            {
                if (dataGridViewContestList.Rows.Count != 0)
                {
                    if (_rowOfContest != -1)
                    {
                        var formShowDivers = new FormShowDivers();
                        if (EventGetDiverRegister != null)
                        {
                            formShowDivers.dataGridViewContestDiverRegister.DataSource =
                                EventGetDiverRegister(_rowOfContest);
                        }
                        formShowDivers.ShowDialog();
                    }
                }
                UpdateDiverChanges();
                UpdateDiverDeletion();
                if (EventGetNumberOfDivers != null)
                    labelNumberOfDivers.Text = EventGetNumberOfDivers(_rowOfContest).ToString();
            }
            if (_showOldContests)
            {
                if (dataGridViewContestList.Rows.Count > 0)
                {
                    if (_rowOfContest != -1)
                    {
                        var formShowDivers = new FormShowDivers();
                        if (EventGetOldDiverRegister != null)
                        {
                            formShowDivers.dataGridViewContestDiverRegister.DataSource =
                                EventGetOldDiverRegister(_rowOfContest);
                        }
                        formShowDivers.Edit = false;
                        formShowDivers.Show();
                    }
                }
            }
        }

        private void buttonShowJudges_Click(object sender, EventArgs e)
        {
            if (!_showOldContests)
            {
                if (dataGridViewContestList.Rows.Count != 0)
                {
                    if (_rowOfContest != -1)
                    {
                        var formShowJudges = new FormShowJudges();
                        if (EventGetJudgeRegister != null)
                        {
                            formShowJudges.dataGridViewContestJudgeRegister.DataSource =
                                EventGetJudgeRegister(_rowOfContest);
                        }
                        formShowJudges.ShowDialog();
                    }
                }
                UpdateJudgeChanges();
                UpdateJudgeDeletion();
                if (EventGetNumberOfJudges != null)
                    labelNumberOfJudges.Text = EventGetNumberOfJudges(_rowOfContest).ToString();
            }
            if (_showOldContests)
            {
                if (dataGridViewContestList.Rows.Count != 0)
                {
                    if (_rowOfContest != -1)
                    {
                        var formShowDivers = new FormShowDivers();
                        if (EventGetOldDiverRegister != null)
                        {
                            formShowDivers.dataGridViewContestDiverRegister.DataSource =
                                EventGetOldDiverRegister(_rowOfContest);
                        }
                        formShowDivers.Edit = false;
                        formShowDivers.Show();
                    }
                }
            }
        }

        private void buttonStartContest_Click(object sender, EventArgs e)
        {
            if (dataGridViewContestList.Rows.Count != 0)
            {
                if (_rowOfContest != -1)
                {
                    var formStartEvent = new FormStartEvent();
                    if ((EventGetJudgeRegister != null) && (EventGetEventList != null))
                    {
                        formStartEvent.JudgeRegister = EventGetJudgeRegister(_rowOfContest);
                        formStartEvent.EventList = EventGetEventList(_rowOfContest);

                    }
                    if (EventGetContest != null)
                    {
                        formStartEvent.ContestName = EventGetContest(_rowOfContest).Name;
                        formStartEvent.Date = EventGetContest(_rowOfContest).StartTime;
                    }
                    if (CheckStartContest() && (EventGetEventList != null))
                    {
                        for (var eventIndex = 0;  eventIndex <= (EventGetEventList(_rowOfContest).Count - 1); eventIndex++)
                        {
                            EventGetEventList(_rowOfContest)[eventIndex].ReadFromJumpAppendix("Divingregister.xlsx");
                            EventGetEventList(_rowOfContest)[eventIndex].RandomizeJumps();
                            EventGetEventList(_rowOfContest)[eventIndex].AddRoundsTojumpList();
                        }
                        formStartEvent.ShowDialog();

                        if ((formStartEvent.DialogResult == DialogResult.OK) && (EventGetEvent != null) && (EventGetContest != null) && (EventReadOldContestListFromXmlFile != null) && (EventSaveNewOldContest != null) && (EventDeleteContest != null) && (EventSaveOldContestListToXmlFile != null))
                        {
                            Html.create_result_page(EventGetEvent(_rowOfContest, _rowOfEvent),
                                EventGetContest(_rowOfContest));
                            EventReadOldContestListFromXmlFile(@"../../view/oldcontests.xml");
                            EventSaveNewOldContest(EventGetContest(_rowOfContest));
                            EventDeleteContest(_rowOfContest);
                            EventSaveOldContestListToXmlFile(@"../../view/oldcontests.xml");

                            dataGridViewContestList_CellClick(dataGridViewContestList,
                                new DataGridViewCellEventArgs(0, _rowOfContest));
                        }
                    }
                }
            }
        }

        private void buttonSaveContestToXmlFile_Click(object sender, EventArgs e)
        {
            if (dataGridViewContestList.Rows.Count != 0)
            {
                if (_rowOfContest != -1)
                {
                    var formSaveContestToFile = new FormSaveAndReadContestAndContestListToAndFromFile
                    {
                        Text = @"Spara tävlingen till fil",
                        Type = "SAVE"
                    };

                    formSaveContestToFile.ShowDialog();
                    if ((EventSaveContestToXmlFile != null) && (formSaveContestToFile.DialogResult == DialogResult.OK))
                    {
                        EventSaveContestToXmlFile(formSaveContestToFile.FileName + ".xml", _rowOfContest);
                    }
                }
            }
        }

        private void buttonAddEventToContest_Click(object sender, EventArgs e)
        {
            if (dataGridViewContestList.Rows.Count != 0)
            {
                if (_rowOfContest != -1)
                {
                    var formAddEvent = new FormAddEvent();
                    formAddEvent.ShowDialog();

                    if ((EventAddEventToContest != null) && (formAddEvent.EventId != -1))
                    {
                        EventAddEventToContest(formAddEvent.EventId, formAddEvent.EventGender, formAddEvent.EventBracket,
                            formAddEvent.EventHeight, formAddEvent.EventRounds, formAddEvent.EventMaxDivers,
                            formAddEvent.EventMaxJudges, formAddEvent.EventStarttime, formAddEvent.EventFinishtime,
                            formAddEvent.EventCouple, _rowOfContest);
                    }
                }
            }
        }

        private void dataGridViewEventList_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridViewEventList.Rows.Count != 0)
            {
                _rowOfEvent = e.RowIndex;
                if (_showOldContests)
                {
                    if (EventGetOldEventList != null)
                    {
                        var fsh = new FormShowHighscore();

                        fsh.ShowHighOldHighScore(EventGetOldEventList(_rowOfContest), _rowOfEvent);
                        fsh.Show();
                    }
                }
            }
        }

        private void buttonDeleteEventFromContest_Click(object sender, EventArgs e)
        {
            if ((dataGridViewContestList.Rows.Count != 0) && (dataGridViewEventList.Rows.Count != 0))
            {
                if ((_rowOfContest != -1) && (_rowOfEvent != -1))
                {
                    if (EventDeleteEventFromContest != null)
                    {
                        EventDeleteEventFromContest(_rowOfContest, _rowOfEvent);
                    }
                }
                if (_rowOfEvent == dataGridViewEventList.Rows.Count)
                {
                    _rowOfEvent--;
                    if (_rowOfEvent != -1)
                    {
                        dataGridViewEventList.Rows[_rowOfEvent].Selected = true;
                    }
                }
                else
                {
                    dataGridViewEventList.Rows[_rowOfEvent].Selected = true;
                }
            }
        }

        private void buttonAddDiverToEvent_Click(object sender, EventArgs e)
        {
            if ((dataGridViewContestList.Rows.Count != 0) && (dataGridViewEventList.Rows.Count != 0))
            {
                if ((_rowOfContest != -1) && (_rowOfEvent != -1))
                {
                    var formAddDiverToEvent = new FormAddDiverToEvent();
                    if (EventGetDiverRegister != null)
                    {
                        formAddDiverToEvent.DiverRegister = EventGetDiverRegister(_rowOfContest);
                    }
                    if (EventGetEvent != null)
                    {
                        formAddDiverToEvent.EventId = EventGetEvent(_rowOfContest, _rowOfEvent).Id;
                        formAddDiverToEvent.EventCoupleBool = EventGetEvent(_rowOfContest, _rowOfEvent).CoupleBool;
                        formAddDiverToEvent.EventGender = EventGetEvent(_rowOfContest, _rowOfEvent).Gender;
                        formAddDiverToEvent.EventBracket = EventGetEvent(_rowOfContest, _rowOfEvent).Bracket;
                        formAddDiverToEvent.EventJumpHeight = EventGetEvent(_rowOfContest, _rowOfEvent).JumpHeight;
                        formAddDiverToEvent.EventRounds = EventGetEvent(_rowOfContest, _rowOfEvent).Rounds;
                        formAddDiverToEvent.EventStartTime = EventGetEvent(_rowOfContest, _rowOfEvent).StartTime;
                        formAddDiverToEvent.EventFinishTime = EventGetEvent(_rowOfContest, _rowOfEvent).FinishTime;
                        formAddDiverToEvent.DiverWithJumpCodes = EventGetEvent(_rowOfContest, _rowOfEvent).DiverList;
                        formAddDiverToEvent.JumpAppendixList = EventGetEvent(_rowOfContest, _rowOfEvent).Jumpappendix;
                        formAddDiverToEvent.JumpHeight = EventGetEvent(_rowOfContest, _rowOfEvent).JumpHeight;
                    }
                    if (EventGetSignedDiversToEvent != null)
                    {
                        for (var i = 0; i < EventGetSignedDiversToEvent(_rowOfContest, _rowOfEvent).Count; i++)
                        {
                            formAddDiverToEvent.SignedToEvent.Add(
                                EventGetSignedDiversToEvent(_rowOfContest, _rowOfEvent)[i].Diver);
                        }
                    }
                    formAddDiverToEvent.ShowDialog();

                    if (formAddDiverToEvent.DialogResult == DialogResult.OK)
                    {
                        if (EventGetEvent != null)
                        {
                            for (var index = EventGetEvent(_rowOfContest, _rowOfEvent).DiverList.Count - 1;
                                index >= 0;
                                index--)
                            {
                                var diverFound =
                                    formAddDiverToEvent.SignedToEvent.SingleOrDefault(
                                        d =>
                                            d.PersonalCodeNumber ==
                                            EventGetEvent(_rowOfContest, _rowOfEvent).DiverList[index].Diver
                                                .PersonalCodeNumber);
                                if (diverFound == null)
                                {
                                    EventGetEvent(_rowOfContest, _rowOfEvent)
                                        .RemoveDiver(
                                            EventGetEvent(_rowOfContest, _rowOfEvent).DiverList[index].Diver
                                                .PersonalCodeNumber);
                                }
                                else
                                {
                                    EventGetEvent(_rowOfContest, _rowOfEvent).DiverList[index].Diver = diverFound;
                                }
                            }

                            for (var index = 0; index < formAddDiverToEvent.SignedToEvent.Count; index++)
                            {
                                
                                var diverFound =
                                    EventGetEvent(_rowOfContest, _rowOfEvent).DiverList.SingleOrDefault(
                                        d =>
                                            d.Diver.PersonalCodeNumber ==
                                            formAddDiverToEvent.SignedToEvent[index].PersonalCodeNumber);
                                if (diverFound == null)
                                {
                                    EventGetEvent(_rowOfContest, _rowOfEvent)
                                        .AddDiver(formAddDiverToEvent.SignedToEvent[index].Name,
                                            formAddDiverToEvent.SignedToEvent[index].Gender,
                                            formAddDiverToEvent.SignedToEvent[index].PersonalCodeNumber,
                                            formAddDiverToEvent.SignedToEvent[index].Nationality,
                                            formAddDiverToEvent.SignedToEvent[index].Club);
                                }
                            }
                        }
                    }
                }
            }
        }

        private void buttonEditEvent_Click(object sender, EventArgs e)
        {
            if ((dataGridViewContestList.Rows.Count != 0) && (dataGridViewEventList.Rows.Count != 0))
            {
                if (_rowOfContest != -1)
                {
                    var formEditEvent = new FormEditEvent();
                    if (EventGetEventList != null)
                    {
                        formEditEvent.dataGridViewEventInformation.DataSource = EventGetEventList(_rowOfContest);
                        formEditEvent.EventList = EventGetEventList(_rowOfContest);
                    }
                    formEditEvent.ShowDialog();
                }
            }
            dataGridViewEventList.Refresh();
        }

        private void dataGridViewContestInformation_CellClick(object sender, DataGridViewCellEventArgs e)
        {
        }

        private void buttonEditContest_Click(object sender, EventArgs e)
        {
            if (dataGridViewContestList.Rows.Count != 0)
            {
                var formEditContest = new FormEditContest
                {
                    ContestList = EventGetContestList()
                };
                if (EventGetContestList != null)
                {
                    formEditContest.dataGridViewChangeContestList.DataSource = EventGetContestList();
                }
                formEditContest.ShowDialog();
            }
            if (EventGetContest != null) //Refresh
            {
                dataGridViewContestInformation[0, 0].Value = EventGetContest(_rowOfContest).Name;
                dataGridViewContestInformation[1, 0].Value = EventGetContest(_rowOfContest).Id;
                dataGridViewContestInformation[2, 0].Value = EventGetContest(_rowOfContest).Location;
                dataGridViewContestInformation[3, 0].Value = EventGetContest(_rowOfContest).StartTime;
                dataGridViewContestInformation[4, 0].Value = EventGetContest(_rowOfContest).FinishTime;
                dataGridViewContestInformation[5, 0].Value = EventGetContest(_rowOfContest).DeadlineDate;
                dataGridViewContestInformation[6, 0].Value = EventGetContest(_rowOfContest).ContestAdministrator;
            }
        }

        private void buttonInfo_Click(object sender, EventArgs e)
        {
            var formInfo = new FormInfo();
            formInfo.ShowDialog();
        }

        private void buttonHelp_Click(object sender, EventArgs e)
        {
            var formHelp = new FormHelp();
            formHelp.ShowDialog();
        }

        private void buttonAddJudgeToEvent_Click(object sender, EventArgs e)
        {
            if ((dataGridViewContestList.Rows.Count != 0) && (dataGridViewEventList.Rows.Count != 0))
            {
                if ((_rowOfContest != -1) && (_rowOfEvent != -1))
                {
                    var formAddJudgeToEvent = new FormAddJudgeToEvent();
                    if (EventGetJudgeRegister != null)
                    {
                        formAddJudgeToEvent.JudgeRegister = EventGetJudgeRegister(_rowOfContest);
                    }
                    if (EventGetEvent != null)
                    {
                        formAddJudgeToEvent.EventId = EventGetEvent(_rowOfContest, _rowOfEvent).Id;
                        formAddJudgeToEvent.EventCoupleBool = EventGetEvent(_rowOfContest, _rowOfEvent).CoupleBool;
                        formAddJudgeToEvent.EventGender = EventGetEvent(_rowOfContest, _rowOfEvent).Gender;
                        formAddJudgeToEvent.EventBracket = EventGetEvent(_rowOfContest, _rowOfEvent).Bracket;
                        formAddJudgeToEvent.EventJumpHeight = EventGetEvent(_rowOfContest, _rowOfEvent).JumpHeight;
                        formAddJudgeToEvent.EventRounds = EventGetEvent(_rowOfContest, _rowOfEvent).Rounds;
                        formAddJudgeToEvent.EventStartTime = EventGetEvent(_rowOfContest, _rowOfEvent).StartTime;
                        formAddJudgeToEvent.EventFinishTime = EventGetEvent(_rowOfContest, _rowOfEvent).FinishTime;

                        formAddJudgeToEvent.SignedToEvent = EventGetEvent(_rowOfContest, _rowOfEvent).JudgeList;
                    }
                    formAddJudgeToEvent.ShowDialog();
                }
            }
        }

        private void buttonSaveContestListToXmlFile_Click(object sender, EventArgs e)
        {
            if (dataGridViewContestList.Rows.Count != 0)
            {
                var formSaveContestListToFile = new FormSaveAndReadContestAndContestListToAndFromFile
                {
                    Text = @"Spara tävlingarna till fil",
                    Type = "SAVE"
                };

                formSaveContestListToFile.ShowDialog();
                if ((EventSaveContestListFromXmlFile != null) &&
                    (formSaveContestListToFile.DialogResult == DialogResult.OK))
                {
                    EventSaveContestListFromXmlFile(formSaveContestListToFile.FileName + ".xml");
                }
            }
        }

        private void buttonReadContestListFromXmlFile_Click(object sender, EventArgs e)
        {
            var formReadContestListFromFile = new FormSaveAndReadContestAndContestListToAndFromFile()
            {
                Text = @"Läs tävlingar från fil",
                Type = "READ"
            };

            formReadContestListFromFile.ShowDialog();
            if ((EventReadContestListFromXmlFile != null) &&
                (formReadContestListFromFile.DialogResult == DialogResult.OK))
            {
                EventReadContestListFromXmlFile(formReadContestListFromFile.FileName + ".xml");
            }
        }

        #endregion

        #region IFormMain Members

        void IFormMain.SetContestList(BindingList<Contest> contests)
        {
            dataGridViewContestList.DataSource = contests;
        }

        public event DelegateSaveOldContestListToXmlFile EventSaveOldContestListToXmlFile;

        public event DelegateReadOldContestListFromXmlFile EventReadOldContestListFromXmlFile;

        public event DelegateNewContest EventNewContest;

        public event DelegateDeleteContest EventDeleteContest;

        public event DelegateStartContest EventStartContest;

        public event DelegateSaveContestToXmlFile EventSaveContestToXmlFile;

        public event DelegateGetNumberOfDivers EventGetNumberOfDivers;

        public event DelegateGetNumberOfJudges EventGetNumberOfJudges;

        public event DelegateGetContest EventGetContest;

        public event DelegateAddDiverToRegister EventAddDiverToRegister;

        public event DelegateAddJudgeToRegister EventAddJudgeToRegister;

        public event DelegateGetDiverRegister EventGetDiverRegister;

        public event DelegateGetJudgeRegister EventGetJudgeRegister;

        public event DelegateAddEventToContest EventAddEventToContest;

        public event DelegateGetEventList EventGetEventList;

        public event DelegateDeleteEventFromContest EventDeleteEventFromContest;

        public event DelegateGetContestList EventGetContestList;

        public event DelegateGetEvent EventGetEvent;

        public event DelegateGetSignedDiversToEvent EventGetSignedDiversToEvent;

        public event DelegateSaveContestListFromXmlFile EventSaveContestListFromXmlFile;

        public event DelegateReadContestListFromXmlFile EventReadContestListFromXmlFile;

        public event DelegateReadContestFromXmlFile EventReadContestFromXmlFile;

        public event DelegateSaveNewOldContest EventSaveNewOldContest;

        public event DelegateGetOldContestList EventGetOldContestList;

        public event DelegateGetOldDiverRegister EventGetOldDiverRegister;

        public event DelegateGetOldJudgeRegister EventGetOldJudgeRegister;

        public event DelegateGetOldNumberOfDivers EventGetOldNumberOfDivers;

        public event DelegateGetOldNumberOfJudges EventGetOldNumberOfJudges;

        public event DelegateGetOldContest EventGetOldContest;

        public event DelegateGetOldEventList EventGetOldEventList;

        public event DelegateGetOldEvent EventGetOldEvent;

        #endregion
    }
}